package com.weijianhuawen.wjblog.web.schedule;

import com.weijianhuawen.wjblog.common.domain.dos.StatisticsArticlePvDo;
import com.weijianhuawen.wjblog.common.domain.mapper.StatisticsArticlePvMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description: 任务定时器
 * @date: 2024-11-01 16:28
 */
@Component
@Slf4j
public class InitPvRecordScheduledTask {
    @Resource
    private StatisticsArticlePvMapper statisticsArticlePvMapper;

    // 每天23:00 定时执行 新增下一日 pv数据
    @Scheduled(cron = "0 0 23 * * ?")
    public void execute() {
        // 定时任务
        log.info("【任务定时器】 定时添加下一日pv数据");
        // 当前日期
        LocalDate currDate = LocalDate.now();
        // 明日
        LocalDate tomorrowDate = currDate.plusDays(1);

        // 插入记录
        StatisticsArticlePvDo statisticsArticlePvDo = StatisticsArticlePvDo
                .builder()
                .pvCount(0L)
                .pvDate(tomorrowDate)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();

        statisticsArticlePvMapper.insert(statisticsArticlePvDo);
        log.info("【任务定时器】 定时任务 添加下一日pv数据 完成");
    }

}
